package com.xinqi.modules.mq.rongyun.util;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.util.Iterator;
import java.util.Map;

public class JSONUtil {

    /**
     * 将JSON的long,转成String,以防止精度丢失
     */
    public static JSONObject long2String(JSONObject jsonObject){

        Iterator iter = jsonObject.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();

            if(entry.getValue() instanceof JSONObject){
                long2String(jsonObject.getJSONObject(entry.getKey().toString()));
            }
            if(entry.getValue() instanceof JSONArray){
                for (int i = 0; i < ((JSONArray) entry.getValue()).size(); i++) {
                    long2String(((JSONArray) entry.getValue()).getJSONObject(i));
                }
            }
            if(entry.getValue() instanceof Long){
                jsonObject.put(entry.getKey().toString(),String.valueOf(entry.getValue()));
            }
        }
        return jsonObject;
    }

}
